﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Health;
using SDS.SPHealthAnalyzerRules.Delegation.Helpers;

namespace SDS.SPHealthAnalyzerRules.Delegation.Rules
{
    /// <summary>
    /// Validates that SPN's exist for webapps with Kerb authentication, for every combination of:
    /// - hostname/FQDN/AAM
    /// - port (if not 80/443)
    /// - service account name
    /// </summary>
    //public class WebApplicationAAMs_Have_IisBindings : SPHealthAnalysisRule
    //{
    //    private Guid CorrelationToken = Guid.NewGuid();

    //    private struct Error
    //    {
    //        public string ServerName;
    //        public string WebAppName;
    //        public string Zone;
    //        public string Url;
    //        public string ErrorMessage;
    //        public string Remedy;
    //    }
    //    private List<Error> Errors = new List<Error>();

    //    public override SPHealthCategory Category { get { return SPHealthCategory.Configuration; } }

    //    public override SPHealthAnalysisRuleAutomaticExecutionParameters AutomaticExecutionParameters
    //    {
    //        get
    //        {
    //            return new SPHealthAnalysisRuleAutomaticExecutionParameters()
    //            {
    //                Schedule = SPHealthCheckSchedule.Daily,
    //                Scope = SPHealthCheckScope.Any,
    //                ServiceType = typeof(SPTimerService),
    //                RepairAutomatically = false
    //            };
    //        }
    //    }

    //    public override SPHealthCheckStatus Check()
    //    {
    //        SPHealthCheckStatus outval = SPHealthCheckStatus.Failed;

    //        LoggingService.Write(LoggingService.Category_WebApp, TraceSeverity.Verbose, "Executing Health Check", CorrelationToken);

    //        // running as the system account
    //        Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate { outval = doCheck(); });

    //        LoggingService.Write(
    //            LoggingService.Category_WebApp,
    //            TraceSeverity.Verbose,
    //            String.Format("Health Check completed with outcome {0}", outval.ToString()),
    //            CorrelationToken);

    //        return outval;
    //    } // void Check()

    //    public override SPHealthCheckErrorLevel ErrorLevel { get { return SPHealthCheckErrorLevel.Error; } }
    //    public override string Summary { get { return "One or more Alternate Access Mappings (AAMs) do not exist"; } }
    //    public override string Explanation
    //    {
    //        get { return String.Format("The following SPN's do not exist:{0} {1}", Environment.NewLine, "http/[servername] domain\\serviceaccount"); }
    //    }
    //    public override string Remedy
    //    {
    //        get { return String.Format("setspn -a http/servername domain\\serviceaccount");  }
    //    }




    //    private SPHealthCheckStatus doCheck()
    //    {

    //        try
    //        {

    //            // loop through each server hosting websites
    //            foreach (var Server in Helpers.SharePoint.Servers().Running<SPWebService>())
    //            {
    //                string ServerName = Server.Name;

    //                // loop through each WebApplication
    //                foreach (var WebApp in Server.WebApplications().UsingKerberos())
    //                {
    //                    //WebApp.AlternateUrls.ToList()

    //                    // loop through each Zone using Kerberos Authentication
    //                    foreach (var IisSetting in WebApp.IisSettings.Select(i => i.Value))
    //                    {
    //                        // check SecureBindings (https)
    //                        foreach (var SecureBinding in IisSetting.SecureBindings)
    //                        {
    //                        }
    //                        // check ServerBindings (http)
    //                        foreach (var ServerBinding in IisSetting.ServerBindings)
    //                        {

    //                        }
    //                    } // foreach Zone
    //                } // foreach WebApp
    //            } // foreach Server

    //        } // try
    //        catch (Exception ex)
    //        {
    //            LoggingService.Write(
    //                LoggingService.Category_WebApp,
    //                TraceSeverity.High,
    //                String.Format("Health Check encountered the following exception: {0}", ex.Message),
    //                CorrelationToken);
    //        }

    //        return SPHealthCheckStatus.Passed;

    //    } // doCheck()
    //} // class


} // namespace
